exports.popularitySearch = function(req,res) {

    //获取restful参数
    var restfulParams = req.routeInfo.restfulParams;

    //最终结果
    var resultData = {};

    var skip  = restfulParams.skip  == undefined ? 0 : restfulParams.skip;
    var limit = restfulParams.limit == undefined ? config.publicConfig.LIMIT : restfulParams.limit;

    //任务流创建
    async.waterfall([

        //1.查询附近机构
        function(cb){

            R.SEND_HTTP(
                req,
                {
                    url    : CORE_APIConfig.coreAPI.searchOrganization + "/skip/" + skip + "/limit/" + limit + "/sort_order/o_star_level-desc",
                    method : 'POST',
                    data : {
                        fields : [      
                            "o_id",
                            "o_name",
                            "o_logo",
                            "o_tags",
                            "o_address",
                            "o_star_level"
                        ].join(",")
                    }
                },
                function (err, data) {
                    //判断是否有错误信息
                    if (err) {
                        return cb(err, data);
                    }

                    resultData.organizationData = data.data;


                    cb();
                }
            );
        },

        //查询每个结构是否有约课课程
        function(cb){

            //创建添加任务
            var tasks = [];

            //循环节点列表
            resultData.organizationData.forEach(function (item, i) {
           
                //创建每一个插入任务
                tasks.push(function (cb) {
                    
                    //查询机构最新的一条约课课程信息
                    
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.search_organization_course + "/oc_oid/ " + item.o_id + "/oc_type/1/skip/0/limit/1",
                            method: 'POST',
                            data: {
                                fields: ["oc_id","oc_title","oc_course_count","oc_new_price"].join(",")
                            }
                        },
                        function (err, data) {
                            //判断是否有错误信息
                            if (err) {
                                return cb(err, data);
                            }
    
                            item.courseData = data.data;
    
                            cb();
                        }
                    );

                });

            });
            async.waterfall(tasks, cb);

        }


        ],function(err, result){

            //返回最终拼接结果
            RES.response(res,true,resultData);

        }
    );

}